<?php
/**
 * communit.as
 * @copyright (C)2008 Jaybill McCarthy, All Rights Reserved.
 * @category communitas
 * @package communitas
 * @author Jaybill McCarthy
 * @link http://communit.as communit.as
 * @license http://communit.as/docs/license License
 */

/**
 *
 * @package communitas
 * @subpackage forum_models
 * @license http://communit.as/docs/license License 
 */
 
class Topics extends Cts_Db_Table_Abstract
{
    protected $_name 	= 'forum_topics';
    protected $_primary = 'id';
    
    function insert(array $data){
    	$data['created_on'] = date(DB_DATETIME_FORMAT);
    	$data['updated_on'] = date(DB_DATETIME_FORMAT);
    	return parent::insert($data);	
    }

    function update(array $data,$where){
    	$data['updated_on'] = date(DB_DATETIME_FORMAT);
    	return parent::update($data,$where);
    }
    
    public function delete($where){
    	$posts_table = new Posts();
    	$topics = $this->fetchAll($where);
    	if(count($topics) > 0){
	    	foreach($topics as $topic){	    		
	    		$posts_where = $this->getAdapter()->quoteInto('topic_id = ?',$topic->id);
	    		$posts_table->delete($posts_where);    		
	    	}
    	}
    	return parent::delete($where);
    }    

	function getTopicById($topic_id){
		$tmp_topic = $this->fetchRow('id = ' . $topic_id);
		if(count($tmp_topic) > 0){
			$tmp_topic = $tmp_topic->toArray();
			return $tmp_topic;
		}else{
			return null;
		}
	}

	function getTopicByPostId($post_id){
		$posts_table = new Posts();
		$tmp_post = $posts_table->fetchRow('id = ' . $post_id);
		if(count($tmp_post > 0)){
			$topic_id = $tmp_post->topic_id;
			$tmp_topic = $this->fetchRow('id = ' . $topic_id);
			if(count($tmp_topic) > 0){
				$tmp_topic = $tmp_topic->toArray();
				return $tmp_topic;
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
        
}
